package com.xx.controller.xunyou.manager;

import com.xx.admin.entity.AdminModule;
import com.xx.admin.entity.AdminUser;
import com.xx.admin.util.ApacheSecurityUtil;
import com.xx.controller.util.BaseController;
import com.xx.springBootUtil.result.JsonResult;
import com.xx.springBootUtil.util.ObjectHelper;
import com.xx.springBootUtil.util.UUIDHelper;
import com.xx.springBootUtil.validate.ValidateWriteIn;
import com.xx.springBootUtil.validate.ValidateWriteInPost;
import com.xx.util.page.PageInfo;
import com.xx.util.result.JsonUtil;
import com.xx.xunyou.dao.item.XunyouItemTypeDao;
import com.xx.xunyou.entity.item.XunyouItemType;
import com.xx.xunyou.service.ItemTypeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: xunyou-parent
 * @author: zhl
 * @create: 2019-04-16
 **/
@RestController
@ApiIgnore
public class ItemTypeController extends BaseController {

    @Autowired
    private ItemTypeService itemTypeService;

    @Resource
    PageInfo pageInfo;

    /**
     * 查询
     *
     * @param parentId
     * @param typeName
     * @param modelAndView
     * @return
     * @throws NumberFormatException
     * @throws Exception
     */
    @RequestMapping("manager/itemType/query")
    public ModelAndView queryList(Integer parentId,String typeName,  ModelAndView modelAndView) throws Exception{
        if(ObjectHelper.isEmpty(parentId)){
            parentId = 0;
        }
        if (parentId > 0) {
            XunyouItemType xunyouItemType = itemTypeService.findByIdToManager(parentId);
            modelAndView.addObject("parent", xunyouItemType);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("parentId", parentId);
        map.put("typeName", typeName);
        Integer totalRow = itemTypeService.queryListCountToManager(map);
        pageInfo.format(totalRow, request); //默认15页
        map.put("index", pageInfo.getPageSize()*(pageInfo.getPageNumber()-1));
        map.put("last", pageInfo.getPageSize());
        List<Map> list = itemTypeService.queryListToManager(map);

        modelAndView.addObject("map", map);
        modelAndView.addObject("list", list);
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.setViewName("manager/itemType/itemTypeList");
        return modelAndView;
    }

    /**
     * 添加信息
     * @return
     * @throws Exception
     */
    @ValidateWriteInPost(parameter={"typeName","typeKey"},msg={"分类名称不能为空！","分类关键字不能为空！"})
    @RequestMapping("manager/itemType/insert")
    public ModelAndView insert(XunyouItemType xunyouItemType) throws Exception{

        if(ObjectHelper.getOrPost(request)){
            setAttr("parentId", xunyouItemType.getParentId()==null?0:xunyouItemType.getParentId());
            return new ModelAndView("manager/itemType/itemTypeInsert");
        }else{
            AdminUser adminUser = sessionAdminUser();
            xunyouItemType.setUserId(adminUser.getUserId());
            itemTypeService.insert(xunyouItemType);

            responseJson(JsonUtil.jsonRefresh("添加成功"));
        }
        return null;
    }

    /**
     * 删除
     */
    @ValidateWriteInPost(parameter={"typeId"},msg={"id不能为空！"})
    @RequestMapping("manager/itemType/delete")
    public JsonResult delete(Integer typeId) throws Exception{
        itemTypeService.deleteById(typeId);
        return JsonUtil.jsonRefresh("删除成功");
    }

    /**
     * 恢复
     */
    @ValidateWriteInPost(parameter={"typeId"},msg={"id不能为空！"})
    @RequestMapping("manager/itemType/recovery")
    public JsonResult recovery(Integer typeId) throws Exception{
        itemTypeService.recoveryById(typeId);
        return JsonUtil.jsonRefresh("恢复成功");
    }

    /**
     * 修改
     */
    @RequestMapping("manager/itemType/update")
    @ValidateWriteIn(parameter={"typeId"},msg={"id不能为空！"})
    public ModelAndView update(XunyouItemType xunyouItemType) throws Exception{

        if(ObjectHelper.getOrPost(request)){
            return new ModelAndView("manager/itemType/itemTypeUpdate")
                    .addObject("xunyouItemType", itemTypeService.findByIdToManager(xunyouItemType.getTypeId()));

        }else{

            itemTypeService.update(xunyouItemType);

            responseJson(JsonUtil.jsonRefresh("修改成功"));
        }
        return null;
    }
}
